// 成就系统
const achievements = {
    list: [
        { id: 'first-break', name: '初级摸鱼师', description: '完成第一个专注时段', unlocked: false },
        { id: 'focus-master', name: '摸鱼高手', description: '完成4个专注时段', unlocked: false },
        { id: 'grand-master', name: '摸鱼大师', description: '完成8个专注时段', unlocked: false },
        { id: 'minesweeper', name: '扫雷高手', description: '成功完成一局扫雷游戏', unlocked: false },
        { id: 'snake-novice', name: '贪吃蛇新手', description: '在贪吃蛇游戏中获得5分以上', unlocked: false },
        { id: 'snake-master', name: '贪吃蛇高手', description: '在贪吃蛇游戏中获得10分以上', unlocked: false },
        { id: 'knowledge', name: '知识渊博', description: '阅读了5个冷知识', unlocked: false },
        { id: 'meditation', name: '内心平静', description: '完成了一次冥想', unlocked: false },
        { id: 'health', name: '健康达人', description: '查看了伸展运动指南', unlocked: false },
        { id: 'fish-king', name: '鱼王之王', description: '解锁所有其他成就', unlocked: false }
    ],
    
    // 初始化成就系统
    init() {
        // 从本地存储加载成就数据
        const savedAchievements = localStorage.getItem('fishtime-achievements');
        if (savedAchievements) {
            const parsedAchievements = JSON.parse(savedAchievements);
            
            // 更新解锁状态
            this.list.forEach((achievement, index) => {
                if (parsedAchievements[index] && parsedAchievements[index].unlocked) {
                    achievement.unlocked = true;
                }
            });
        }
        
        // 更新成就计数
        this.updateAchievementCount();
    },
    
    // 解锁成就
    unlock(id) {
        const achievement = this.list.find(a => a.id === id);
        if (achievement && !achievement.unlocked) {
            achievement.unlocked = true;
            
            // 保存到本地存储
            localStorage.setItem('fishtime-achievements', JSON.stringify(this.list));
            
            // 更新成就计数
            this.updateAchievementCount();
            
            // 检查是否解锁了所有成就
            this.checkAllAchievements();
            
            return achievement;
        }
        return null;
    },
    
    // 检查是否解锁了所有成就
    checkAllAchievements() {
        const allUnlocked = this.list.slice(0, -1).every(a => a.unlocked);
        
        if (allUnlocked && !this.list[this.list.length - 1].unlocked) {
            this.unlock('fish-king');
        }
    },
    
    // 更新成就计数
    updateAchievementCount() {
        const unlockedCount = this.list.filter(a => a.unlocked).length;
        const totalCount = this.list.length;
        
        document.getElementById('achievement-count').textContent = `${unlockedCount}/${totalCount}`;
    },
    
    // 显示成就列表
    showList() {
        console.log('成就列表:');
        this.list.forEach(a => {
            console.log(`${a.name} - ${a.description} - ${a.unlocked ? '已解锁' : '未解锁'}`);
        });
    }
};

// 显示成就弹窗
function showAchievementPopup(name, description) {
    // 查找对应的成就
    const achievementId = achievements.list.find(a => a.name === name)?.id;
    
    if (achievementId) {
        // 尝试解锁成就
        const achievement = achievements.unlock(achievementId);
        
        // 如果成就已解锁，不再显示弹窗
        if (!achievement) return;
    }
    
    const popup = document.getElementById('achievement-popup');
    document.getElementById('achievement-name').textContent = `${name} - ${description}`;
    
    popup.classList.add('show');
    
    // 5秒后自动关闭
    setTimeout(() => {
        popup.classList.remove('show');
    }, 5000);
}

// 初始化成就系统
document.addEventListener('DOMContentLoaded', () => {
    achievements.init();
    
    // 关闭成就弹窗
    document.getElementById('close-achievement').addEventListener('click', () => {
        document.getElementById('achievement-popup').classList.remove('show');
    });
});
// 添加成就查看界面
function initAchievementView() {
    // 创建成就按钮
    const achievementBtn = document.createElement('button');
    achievementBtn.innerHTML = '<i class="fas fa-trophy"></i>';
    achievementBtn.className = 'view-achievements-btn';
    achievementBtn.title = '查看成就';
    
    // 添加到页脚成就部分
    document.querySelector('.achievements').appendChild(achievementBtn);
    
    // 创建成就面板
    const achievementPanel = document.createElement('div');
    achievementPanel.className = 'achievements-panel';
    achievementPanel.innerHTML = `
        <div class="achievements-header">
            <h3>我的成就</h3>
            <button class="close-achievements"><i class="fas fa-times"></i></button>
        </div>
        <div class="achievements-body">
            <div class="achievements-list">
                ${achievements.list.map(a => `
                    <div class="achievement-item ${a.unlocked ? 'unlocked' : ''}">
                        <div class="achievement-icon">
                            <i class="fas fa-trophy"></i>
                        </div>
                        <div class="achievement-info">
                            <h4>${a.name}</h4>
                            <p>${a.description}</p>
                        </div>
                        <div class="achievement-status">
                            ${a.unlocked ? '<i class="fas fa-check-circle"></i>' : '<i class="fas fa-lock"></i>'}
                        </div>
                    </div>
                `).join('')}
            </div>
        </div>
    `;
    
    // 添加到页面
    document.querySelector('.app-container').appendChild(achievementPanel);
    
    // 打开成就面板
    achievementBtn.addEventListener('click', () => {
        // 更新成就列表
        const achievementsList = document.querySelector('.achievements-list');
        achievementsList.innerHTML = achievements.list.map(a => `
            <div class="achievement-item ${a.unlocked ? 'unlocked' : ''}">
                <div class="achievement-icon">
                    <i class="fas fa-trophy"></i>
                </div>
                <div class="achievement-info">
                    <h4>${a.name}</h4>
                    <p>${a.description}</p>
                </div>
                <div class="achievement-status">
                    ${a.unlocked ? '<i class="fas fa-check-circle"></i>' : '<i class="fas fa-lock"></i>'}
                </div>
            </div>
        `).join('');
        
        achievementPanel.classList.add('show');
    });
    
    // 关闭成就面板
    document.querySelector('.close-achievements').addEventListener('click', () => {
        achievementPanel.classList.remove('show');
    });
}